using System.Collections.Generic;
using UnityEngine;

public class PlayerStateBehavior_Shoot : AbstractPlayerStateBehavior
{
	public static PlayerStateBehavior_Shoot instance;

	private PlayerStateBehavior_Shoot()
	{
	}

	public static PlayerStateBehavior_Shoot getInstance()
	{
		if (instance == null)
		{
			instance = new PlayerStateBehavior_Shoot();
		}
		return instance;
	}

	public override void enter(Player owner)
	{
		base.enter(owner);
		owner.shoot_is_dunk = false;
		owner.shoot_is_layup = false;
		if (owner.teamType == TeamType.USER)
		{
			Instance<Court>.instance.count_PauseData_User[0]++;
		}
		else
		{
			Instance<Court>.instance.count_PauseData_Com[0]++;
		}
		if (!DSS.IsCanDunk(owner) && !DSS.IsCanLayUp(owner))
		{
			playAnimation_Shoot(owner);
		}
		else if (!DSS.IsCanDunk(owner) && DSS.IsCanLayUp(owner))
		{
			float num = Random.Range(0f, 100f);
			if (owner.IsFastBreak() && owner.teamType == TeamType.USER)
			{
				checkIsMirror(owner, isDunk: false);
				playAnimation_Layup(owner);
			}
			else if (num < owner.team.percent_shootlayup_shoot)
			{
				playAnimation_Shoot(owner);
			}
			else
			{
				checkIsMirror(owner, isDunk: false);
				playAnimation_Layup(owner);
			}
		}
		else if (DSS.IsCanDunk(owner) && !DSS.IsCanLayUp(owner))
		{
			float num2 = Random.Range(0f, 100f);
			if (owner.IsFastBreak() && owner.teamType == TeamType.USER)
			{
				checkIsMirror(owner, isDunk: true);
				playAnimation_Dunk(owner);
			}
			else if (num2 < owner.team.percent_shootdunk_shoot)
			{
				playAnimation_Shoot(owner);
			}
			else
			{
				checkIsMirror(owner, isDunk: true);
				playAnimation_Dunk(owner);
			}
		}
		else if (DSS.IsCanDunk(owner) && DSS.IsCanLayUp(owner))
		{
			float num3 = Random.Range(0f, 100f);
			if (owner.IsFastBreak() && owner.teamType == TeamType.USER)
			{
				checkIsMirror(owner, isDunk: true);
				playAnimation_Dunk(owner);
			}
			else if (num3 < owner.team.percent_shootdunklayup_shoot)
			{
				playAnimation_Shoot(owner);
			}
			else if (num3 >= owner.team.percent_shootdunklayup_shoot && num3 < owner.team.percent_shootdunklayup_layup)
			{
				checkIsMirror(owner, isDunk: false);
				playAnimation_Layup(owner);
			}
			else
			{
				checkIsMirror(owner, isDunk: true);
				playAnimation_Dunk(owner);
			}
		}
		else
		{
			LogUtils.Error("not find shoot way");
		}
		owner.LookAt(owner.getOffenseBasketPos());
		if (owner.shoot_is_layup || owner.shoot_is_dunk)
		{
			owner.isRootMotion = true;
			if (owner.teamType == TeamType.COMPUTER)
			{
				owner.StartCoroutine("rootMotionCom");
			}
		}
		owner.getBallInHand();
		Instance<Ball>.instance.OnShoot(owner, owner.shoot_is_dunk, owner.shoot_is_layup, Instance<Court>.instance.isInState(CourtStateFreeThrow.getInstance()));
		if (owner.teamType == TeamType.USER)
		{
			Instance<UIGame>.instance.isLockPauseBtn = false;
			Instance<UIGame>.instance.isLockPassBtn = true;
			Instance<UIGame>.instance.isLockShootBtn = true;
			Instance<UIGame>.instance.isLockJoystick = true;
		}
		//if (TutorialController.preTutorialType == TutorialType.Game_LayUp || TutorialController.preTutorialType == TutorialType.Game_Shoot)
		//{
		//	Instance<UIGame>.instance.isLockPauseBtn = true;
		//	Instance<UIGame>.instance.isLockPassBtn = true;
		//	Instance<UIGame>.instance.isLockShootBtn = true;
		//	Instance<UIGame>.instance.isLockJoystick = true;
		//}
	}

	public override void fixedUpdate(Player owner)
	{
		base.fixedUpdate(owner);
		if (!owner.IsPlayingAnimation())
		{
			owner.do_idle();
		}
		else if (owner.shoot_is_dunk || owner.shoot_is_layup)
		{
			if (owner.shoot_is_dunk)
			{
				if (MathHelper.getSqrDistance(owner.getPosition(), (owner.getOffenseDirection() != OffenseDirection.RIGHT) ? Global.posBallIn_Left_Ground : Global.posBallIn_Right_Ground) > owner.shoot_target_sqr_distance)
				{
					GameHelper.MoveTo(owner, owner.myTransform.forward + owner.getPosition(), Global.NormalMoveSpeed_Player * 0.5f, isLookAtTargetPos: true);
				}
			}
			else if (owner.shoot_is_layup)
			{
				if (owner.shoot_layup_is_far)
				{
					if (MathHelper.getSqrDistance(owner.getPosition(), (owner.getOffenseDirection() != OffenseDirection.RIGHT) ? Global.posBallIn_Left_Ground : Global.posBallIn_Right_Ground) > owner.shoot_target_sqr_distance)
					{
						GameHelper.MoveTo(owner, owner.myTransform.forward + owner.getPosition(), Global.NormalMoveSpeed_Player * 0.333f, isLookAtTargetPos: true);
					}
				}
				else if (MathHelper.getSqrDistance(owner.getPosition(), (owner.getOffenseDirection() != OffenseDirection.RIGHT) ? Global.posBallIn_Left_Ground : Global.posBallIn_Right_Ground) < owner.shoot_target_sqr_distance)
				{
					GameHelper.MoveTo(owner, -owner.myTransform.forward + owner.getPosition(), Global.NormalMoveSpeed_Player * 0.25f, isLookAtTargetPos: false);
				}
			}
		}
		if (owner.shoot_is_dunk)
		{
			if (owner.heightType == HeightType.BIG)
			{
				if (Instance<Ball>.instance.isInState(BallState_Shoot.getInstance()))
				{
					Vector3 position = owner.myTransform.position;
					if (position.y > 0f - Global.HeightOffest_Big)
					{
						owner.myTransform.position = MathHelper.setOffestY(owner.getPosition(), -0.02f);
					}
				}
				else
				{
					Vector3 position2 = owner.myTransform.position;
					if (position2.y < Global.HeightOffest_Big)
					{
						Vector3 position3 = owner.trans_RightHand.position;
						if (position3.y < Global.posBallIn_Left_Basketry.y - 0.2f)
						{
							owner.myTransform.position = MathHelper.setOffestY(owner.getPosition(), 0.02f);
						}
					}
				}
			}
			else if (owner.heightType == HeightType.SMALL)
			{
				if (Instance<Ball>.instance.isInState(BallState_Shoot.getInstance()))
				{
					Vector3 position4 = owner.myTransform.position;
					if (position4.y < Global.HeightOffest_Small)
					{
						owner.myTransform.position = MathHelper.setY(owner.getPosition(), 0.02f);
					}
				}
				else
				{
					Vector3 position5 = owner.myTransform.position;
					if (position5.y > 0f - Global.HeightOffest_Small)
					{
						Vector3 position6 = owner.trans_RightHand.position;
						if (position6.y < Global.posBallIn_Left_Basketry.y - 0.2f)
						{
							owner.myTransform.position = MathHelper.setY(owner.getPosition(), -0.02f);
						}
					}
				}
			}
		}
		if (owner.isRootMotion && Physics.Raycast(MathHelper.setY(owner.trans_RootBone.position, 1f), MathHelper.setY(owner.getOffenseBasketPos(), 1f) - MathHelper.setY(owner.trans_RootBone.position, 1f), out RaycastHit hitInfo, 0.9f))
		{
			Player component = hitInfo.transform.parent.GetComponent<Player>();
			if (component != null)
			{
				GameHelper.MoveTo(component, owner.getOffenseBasketPos(), Global.NormalMoveSpeed_Player, isLookAtTargetPos: false);
			}
		}
	}

	public override void exit(Player owner)
	{
		base.exit(owner);
		owner.isRootMotion = false;
		if (owner.teamType == TeamType.COMPUTER)
		{
			owner.StopCoroutine("rootMotionCom");
			if (owner.ojShadow != null)
			{
				owner.ojShadow.transform.localPosition = Vector3.zero;
			}
		}
		owner.setPositon(MathHelper.setY(owner.trans_RootBone.position, 0f));
		owner.LookAt((owner.getOffenseDirection() != 0) ? Global.posBallIn_Right_Ground : Global.posBallIn_Left_Ground);
		owner.setNormalAnimation();
		owner.shoot_is_dunk = false;
		owner.shoot_is_layup = false;
		if (owner.teamType == TeamType.USER && !Instance<Court>.instance.isInState(CourtStateFreeThrow.getInstance()))
		{
			Instance<UIGame>.instance.isLockPauseBtn = false;
			Instance<UIGame>.instance.isLockPassBtn = false;
			Instance<UIGame>.instance.isLockShootBtn = false;
			Instance<UIGame>.instance.isLockJoystick = false;
		}
		//if (TutorialController.preTutorialType == TutorialType.Game_LayUp || TutorialController.preTutorialType == TutorialType.Game_Shoot)
		//{
		//	Instance<UIGame>.instance.isLockPauseBtn = true;
		//	Instance<UIGame>.instance.isLockPassBtn = true;
		//	Instance<UIGame>.instance.isLockShootBtn = true;
		//	Instance<UIGame>.instance.isLockJoystick = true;
		//}
	}

	private void checkIsMirror(Player owner, bool isDunk)
	{
		if (isDunk)
		{
			if (owner.getOffenseDirection() == OffenseDirection.LEFT)
			{
				Vector3 position = owner.getPosition();
				if (position.x > 0f)
				{
					goto IL_004d;
				}
			}
			if (owner.getOffenseDirection() == OffenseDirection.RIGHT)
			{
				Vector3 position2 = owner.getPosition();
				if (!(position2.x < 0f))
				{
					return;
				}
				goto IL_004d;
			}
			return;
		}
		if (Random.Range(0f, 100f) < 50f)
		{
			owner.setMirrorAnimation();
		}
		return;
		IL_004d:
		owner.setMirrorAnimation();
	}

	private void playAnimation_Shoot(Player owner)
	{
		float num = Vector3.SqrMagnitude((owner.getOffenseDirection() != 0) ? (Global.posBallIn_Right_Ground - owner.getPosition()) : Global.posBallIn_Left_Ground);
		if (num <= 1.21f)
		{
			if (MathHelper.isInPercent(owner.team.percent_shoot_close))
			{
				if (owner.getOffenseDirection() == OffenseDirection.LEFT)
				{
					Vector3 position = owner.getPosition();
					if (position.x < 0f)
					{
						goto IL_0092;
					}
				}
				if (owner.getOffenseDirection() == OffenseDirection.RIGHT)
				{
					Vector3 position2 = owner.getPosition();
					if (position2.x > 0f)
					{
						goto IL_0092;
					}
				}
				goto IL_0098;
			}
			owner.playAnimation(AnimationName.player_shoot_early, 1f, 0.5f);
			return;
		}
		owner.playAnimation(AnimationName.player_shoot_early, 1f, 0.5f);
		return;
		IL_0092:
		owner.setMirrorAnimation();
		goto IL_0098;
		IL_0098:
		owner.playAnimation(AnimationName.player_shoot_close, 1f, 0.5f);
	}

	private void playAnimation_Dunk(Player owner)
	{
		List<DunkType> list = new List<DunkType>();
		for (int i = 0; i < 8; i++)
		{
			if (DSS.IsInDunkArea(owner, (DunkType)i) && owner.isHaveDunkType((DunkType)i))
			{
				list.Add((DunkType)i);
			}
		}
		switch (list[Random.Range(0, list.Count)])
		{
		case DunkType.NORMAL:
			owner.playAnimation(AnimationName.player_shoot_dunk, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 2.975625f;
			break;
		case DunkType.TWOHAND:
			owner.playAnimation(AnimationName.player_shoot_dunk_close_twohand, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 1.265625f;
			break;
		case DunkType.ONEHAND:
			owner.playAnimation(AnimationName.player_shoot_dunk_close_onehand, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 1.21f;
			break;
		case DunkType.CROSSLEG:
			owner.playAnimation(AnimationName.player_shoot_dunk_crossleg, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 15.210001f;
			break;
		case DunkType.TURNAROUND:
			owner.playAnimation(AnimationName.player_shoot_dunk_turnaround, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 11.7306242f;
			break;
		case DunkType.BACK:
			owner.playAnimation(AnimationName.player_shoot_dunk_back, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 22.3256245f;
			break;
		case DunkType.CROSSLEG2:
			owner.playAnimation(AnimationName.player_shoot_dunk_crossleg2, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 15.4056244f;
			break;
		case DunkType.ONEHAND_FAR:
			owner.playAnimation(AnimationName.player_shoot_dunk_far_onehand, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 11.055625f;
			break;
		}
		owner.shoot_is_dunk = true;
	}

	private void playAnimation_Layup(Player owner)
	{
		List<LayupType> list = new List<LayupType>();
		for (int i = 0; i < 5; i++)
		{
			if (DSS.IsInLayupArea(owner, (LayupType)i) && owner.isHaveLayupType((LayupType)i))
			{
				list.Add((LayupType)i);
			}
		}
		switch (list[Random.Range(0, list.Count)])
		{
		case LayupType.NORMAL:
			owner.playAnimation(AnimationName.player_shoot_layup, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 24.01f;
			break;
		case LayupType.BACK:
			owner.playAnimation(AnimationName.player_shoot_layup_back, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 16f;
			break;
		case LayupType.EUROSTEP:
			owner.playAnimation(AnimationName.player_shoot_layup_eurostep, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 9f;
			break;
		case LayupType.TURNAROUND:
			owner.playAnimation(AnimationName.player_shoot_layup_turnaround, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 16f;
			break;
		case LayupType.THROW:
			owner.playAnimation(AnimationName.player_shoot_layup_throw, 0.8f, 0.5f);
			owner.shoot_target_sqr_distance = 30.25f;
			break;
		}
		owner.shoot_layup_is_far = (MathHelper.getSqrDistance(owner.getPosition(), (owner.getOffenseDirection() != OffenseDirection.RIGHT) ? Global.posBallIn_Left_Ground : Global.posBallIn_Right_Ground) > owner.shoot_target_sqr_distance);
		owner.shoot_is_layup = true;
	}
}
